;$(function () {
    let holdInterval = null;
    let point = 1;
    let clc = function () {
        let holdInterval = null;
        let point = 1;
        holdInterval = setInterval(function () {
           if (point > 5) {
                point = 1
            }
            $('#qrcode').text('正在获取' + '.'.repeat(point));
            point += 1;
        }, 1000);
        check = function (uuid) {
            $.ajax({
                url: '/front/checklogin/',
                type: 'get',
                data: {
                    uuid: uuid
                },
                success: function (data) {
                    if (data.code === 200) {
                        clearInterval(holdInterval);
                        $('#qrcode').text('登陆成功');
                        $.ajax({
                            url:'/front/send/',
                            data:{'id': $('#template').val(), 'uuid': uuid},
                            type:'get',
                            success:(data)=>{
                                console.log(data)
                                $('#message').text(data.message);
                                $('#chatcode').attr('src', '/static/img/default.jpg');
                                $('#qrcode').text('发送成功 再次获取').one('click', clc);
                            },
                            error:(error)=>{
                                console.log(error)
                                $('#chatcode').attr('src', '/static/img/default.jpg');
                                $('#qrcode').text('网络错误 再次获取').one('click', clc);
                            }
                        })
                    } else if (data.code === 201) {
                        $('#qrcode').text('请允许登陆网页微信').one('click', clc);
                        clearInterval(holdInterval);
                        setTimeout(check, 2000, uuid);
                    } else if (point <= 0) {
                        $('#qrcode').text('登陆超时').one(clc);
                        clearInterval(holdInterval);
                        $('#chatcode').attr('src', '/static/img/default.jpg');

                    } else if (data.code === 101) {
                        setTimeout(check, 2000, uuid);
                    } else {
                        $('#qrcode').one(clc);
                        clearInterval(holdInterval);
                        $('#chatcode').attr('src', '/static/img/default.jpg');
                    }
                }

            })

        };
        $.ajax({
            url: '/front/qrcode/',
            type: 'get',
            data: {},
            success: function (data) {
                if (data.code === 200) {
                    point = 30;
                    clearInterval(holdInterval);

                    $('#qrcode').text('获取成功');
                    $('#chatcode').attr('src', data.data.qrcode);
                    setTimeout(check, 2, data.data.uuid);
                    holdInterval = setInterval(function () {
                        if (point <= 0) {
                            $('#qrcode').text('二维码过期').one('click', clc);
                            clearInterval(holdInterval);
                            socket.disconnect();
                            socket = null;
                            $('#chatcode').attr('src', '/static/img/default.jpg');
                        } else {
                            $('#qrcode').text('二维码还有' + point.toString() + '过期 请尽快扫描');
                            point -= 1;

                        }

                    }, 1000);
                } else {
                    swal({
                        'text': '获取失败'
                    });
                    $('#qrcode').text('再次获取').one('click', clc);
                }
            },
            error() {
                swal({
                    'text': '获取失败'
                });
                $('#qrcode').text('再次获取').one('click', clc);
            }
        });


    };
    $('#qrcode').one('click', clc);


});
